import { Injectable } from '@nestjs/common'
import { User } from '../entities/User.entity'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'

/* 返回数据 */
/* 实现对 user 表的增删改查 操作 */
@Injectable()
export class UserService {
  constructor( 
    // 获取数据库 Model 实现操作数据
    @InjectRepository( User )
    // Repository<XXX> 连接表
    private readonly userRepository: Repository<User>
  ) { }
  
  // 当 user 带有ID时，save = update；当user没有ID时，save = add
  async save(user: User): Promise<any> {
    let res = await this.userRepository.save( user )
    console.log("<--- all user --->", user)
    return res
  }

  // 获取全部数据
  async getAll(option: any): Promise<any> {
    let res = await this.userRepository.find()
    // console.log("<--- all user --->", res, option)
    for (let i = 0; i < 20000; i++) {
      // console.log(i)
    }
    return {
      code: 0,
      data: [...res],
      message: '成功！！！'
    }
  }

  // post 测试用法
  async postTest(data: any): Promise<any> {
    // let res = await this.userRepository.
    // console.log('<--- post test --->', data)
    return '测试数据！！！'
  }
}
